package com.qh.product.service.impl;

import com.qh.basic.service.IBaiduAiAuditService;
import com.qh.basic.util.AjaxResult;
import com.qh.product.domain.Product;
import com.qh.product.domain.ProductDetail;
import com.qh.product.domain.ProductOnlineAuditLog;
import com.qh.product.mapper.ProductDetailMapper;
import com.qh.product.mapper.ProductMapper;
import com.qh.product.mapper.ProductOnlineAuditLogMapper;
import com.qh.product.service.IProductService;
import com.qh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 添加重构
     * @param product
     */
    @Override
    public void save(Product product) {
        //先添加product
        super.save(product);
        //再添加detail
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.save(productDetail);
    }

    /**
     * 修改重构
     * @param product
     */
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateDetailByPetId(productDetail);
        }
    }

    /**
     * 删除重构
     * @param id
     */
    @Override
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delDetailByProductId(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //1.将每个服务查出来
            Product product = productMapper.loadOne(id);
            //2.通过百度AI智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            if (boo1 && boo2){//审核通过
                //修改状态
                productMapper.onsale(id,new Date());
                //添加审核日志
                log.setState(1);
                log.setProductId(id);
                log.setNote("审核成功");
                productOnlineAuditLogMapper.save(log);
            }else {//审核失败
                log.setState(0);
                log.setProductId(id);
                log.setNote("审核失败");
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        productMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
